<?php
namespace api\web\controller;

use api\web\model\ArticleNoticeModel;
use api\web\model\ArticleWorkModel;
use api\web\model\BannerModel;
use api\web\model\NavModel;
use api\web\model\ArticleEductionModel;
use api\web\model\ArticleStudentModel;
use api\web\model\ArticlePolicyModel;
use cmf\controller\RestBaseController;
use api\web\model\ReportModel;
use api\web\model\InfoModel;
use think\facade\Db;

// 首页接口
class IndexController extends RestBaseController
{
    // 获取导航菜单列表
    public function navList()
    {
        $nav = NavModel::order(['list_order' => 'desc'])
            ->field('id,name,url')
            ->where('status', '=', 1)
            ->select();
        $this->success('导航菜单列表', $nav);
    }
    // 获取轮播图列表
    public function getBannerList()
    {
        $banner = BannerModel::order(['list_order' => 'desc'])
            ->field('id,image,url')
            ->where('status', '=', 1)
            ->select();
        // 处理图片地址
        foreach ($banner as $k => $v) {
            $banner[$k]['image'] = cmf_get_image_url($banner[$k]['image']);
        }
        $this->success('轮播图列表', $banner);
    }
    // 获取工作动态新闻列表
    public function getArticleWorkList()
    {
        // 接收每页显示的条数（默认每页查询 10 条）
        $pageSize = $this->request->param('page_size/d', 10);
        // 接收页数（默认第 1 页）
        $page = $this->request->param('page/d', 1);
        // 根据 list_order 字段的值排序，若此字段的值相同，再按照 create_time 字段的值排序
        $list = ArticleWorkModel::order(['list_order' => 'desc', 'create_time' => 'desc'])
            ->field('id,title,abstract,image,author,create_time')
            ->where('status', '=', 1)
            // 分页查询
            ->page($page, $pageSize)
            ->select();
        $this->success('工作动态新闻列表', $list);
    }
    // 获取工作动态文章详情
    public function getArticleWorkDetail()
    {
        $id = $this->request->param('id/d');
        if (!$id) {
            $this->error('参数错误，id 不能为空！');
        }
        // withoutField 用于排除一些字段，不查询
        $article = ArticleWorkModel::withoutField('list_order,status,delete_time,update_time')->find($id);
        if (!$article) {
            $this->error('文章没找到，再看看其它的吧~');
        }
        // 处理富文本
        $article['content'] = cmf_replace_content_file_url(htmlspecialchars_decode($article['content']));
        // 递增浏览量
        Db::name('article_work')->where('id', '=', $id)->inc('read_num')->update();
        $this->success('文章详情', $article);
    }
    // 获取通知公告文章列表
    public function getArticleNoticeList()
    {
        $pageSize = $this->request->param('page_size/d', 10);
        $page = $this->request->param('page/d', 1);
        $list = ArticleNoticeModel::order(['list_order' => 'desc', 'create_time' => 'desc'])
            ->field('id,title,abstract,image,author,create_time')
            ->where('status', '=', 1)
            ->page($page, $pageSize)
            ->select();
        $this->success('通知公告文章列表', $list);
    }
    // 获取通知公告文章详情
    public function getArticleNoticeDetail()
    {
        $id = $this->request->param('id/d');
        if (!$id) {
            $this->error('参数错误，id 不能为空！');
        }
        // withoutField 用于排除一些字段，不查询
        $article = ArticleNoticeModel::withoutField('list_order,status,delete_time,update_time')->find($id);
        if (!$article) {
            $this->error('文章没找到，再看看其它的吧~');
        }
        // 处理富文本
        $article['content'] = cmf_replace_content_file_url(htmlspecialchars_decode($article['content']));
        // 递增浏览量
        Db::name('article_notice')->where('id', '=', $id)->inc('read_num')->update();
        $this->success('文章详情', $article);
    }
    // 获取教育文章列表
    public function getArticleEductionList()
    {
        $pageSize = $this->request->param('page_size/d', 10);
        $page = $this->request->param('page/d', 1);
        $list = ArticleNoticeModel::order(['list_order' => 'desc', 'create_time' => 'desc'])
            ->field('id,title,abstract,image,author,create_time')
            ->where('status', '=', 1)
            ->page($page, $pageSize)
            ->select();
        $this->success('廉政教育文章列表', $list);
    }
    // 获取教育文章详情
    public function getArticleEductionDetail()
    {
        $id = $this->request->param('id/d');
        if (!$id) {
            $this->error('参数错误，id 不能为空！');
        }
        // withoutField 用于排除一些字段，不查询
        $article = ArticleNoticeModel::withoutField('list_order,status,delete_time,update_time')->find($id);
        if (!$article) {
            $this->error('文章没找到，再看看其它的吧~');
        }
        // 处理富文本
        $article['content'] = cmf_replace_content_file_url(htmlspecialchars_decode($article['content']));
        // 递增浏览量
        Db::name('article_eduction')->where('id', '=', $id)->inc('read_num')->update();
        $this->success('文章详情', $article);
    }
    // 获取学习文章列表
    public function getArticleStudentList()
    {
        $pageSize = $this->request->param('page_size/d', 10);
        $page = $this->request->param('page/d', 1);
        $list = ArticleNoticeModel::order(['list_order' => 'desc', 'create_time' => 'desc'])
            ->field('id,title,abstract,image,author,create_time')
            ->where('status', '=', 1)
            ->page($page, $pageSize)
            ->select();
        $this->success('理论学习文章列表', $list);
    }
    // 获取学习文章详情
    public function getArticleStudentDetail()
    {
        $id = $this->request->param('id/d');
        if (!$id) {
            $this->error('参数错误，id 不能为空！');
        }
        // withoutField 用于排除一些字段，不查询
        $article = ArticleNoticeModel::withoutField('list_order,status,delete_time,update_time')->find($id);
        if (!$article) {
            $this->error('文章没找到，再看看其它的吧~');
        }
        // 处理富文本
        $article['content'] = cmf_replace_content_file_url(htmlspecialchars_decode($article['content']));
        // 递增浏览量
        Db::name('article_student')->where('id', '=', $id)->inc('read_num')->update();
        $this->success('文章详情', $article);
    }
    // 获取文章列表
    public function getArticlePolicyList()
    {
        $pageSize = $this->request->param('page_size/d', 10);
        $page = $this->request->param('page/d', 1);
        $list = ArticleNoticeModel::order(['list_order' => 'desc', 'create_time' => 'desc'])
            ->field('id,title,abstract,image,author,create_time')
            ->where('status', '=', 1)
            ->page($page, $pageSize)
            ->select();
        $this->success('政策法规文章列表', $list);
    }
    // 获取文章详情
    public function getArticlePolicyDetail()
    {
        $id = $this->request->param('id/d');
        if (!$id) {
            $this->error('参数错误，id 不能为空！');
        }
        // withoutField 用于排除一些字段，不查询
        $article = ArticleNoticeModel::withoutField('list_order,status,delete_time,update_time')->find($id);
        if (!$article) {
            $this->error('文章没找到，再看看其它的吧~');
        }
        // 处理富文本
        $article['content'] = cmf_replace_content_file_url(htmlspecialchars_decode($article['content']));
        // 递增浏览量
        Db::name('article_policy')->where('id', '=', $id)->inc('read_num')->update();
        $this->success('文章详情', $article);
    }
    //获取信访举报列表
    public function getReportList()
    {
        $report = ReportModel::order('list_order','desc')
            ->field('id,name,url')
            ->where('status','=',1)
            ->select();
        $this->success('获取信访举报列表',$report);
    }
    //获取信息列表
    public function getInfoList()
    {
        $info = InfoModel::order('list_order','desc')
            ->field('id,name,details')
            ->where('status','=',1)
            ->select();
        $this->success('获取信息列表',$info);
    }
}